<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button onclick="start()">开始链接</button>
    <!-- <button onclick="startContect()">建立消息</button> -->
    <button onclick="sendMessage()">发送消息</button>
    <button onclick="leave()">离开房间</button>
    <script>
      let ws = null
      function start() {
        console.log('zzz')
        ws = new WebSocket('ws://127.0.0.1:8080/socket/11')
        ws.onmessage = msg => {
          console.log(JSON.parse(msg.data))
        }
        ws.onerror = msg => {
          console.log(msg)
        }
        ws.onclose = msg => {
          console.log(msg)
        }
        ws.onopen = msg => {
          console.log(msg)
        }
      }
      // function startContent() {
      //   ws.open()
      // }
      function sendMessage() {
        let a = JSON.stringify({ userId: 11, message: 'fffzz', toUserId: 12 })

        ws.send(a)
      }
      function leave() {
        ws.close()
      }
    </script>
  </body>
</html>
